package gases.as3.librarys.compnents.ui.control
{
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import gases.as3.librarys.compnents.events.TreeEvent;
	import gases.as3.librarys.compnents.skin.data.BaseSkin;
	import gases.as3.librarys.compnents.skin.data.TreeSkin;
	import gases.as3.librarys.compnents.ui.GUIComepments;
	import gases.as3.librarys.compnents.ui.control.treeclasses.TreeCellRenderer;
	import gases.as3.librarys.compnents.ui.control.treeclasses.TreeCellRendererVO;
	
	/**
	 *树 
	 * @author Administrator
	 * 
	 */	
	public class Tree extends GUIComepments {
		public static const NODE_OPEN:String = "nodeOpen";
		public static const NODE_CLOSE:String = "nodeClose";
		public var maxLevel:int;
		
		protected var xmlProvider:XML;
		/**
		 *Array<TreeCellRendererVO>
		 */
		protected var treeCellRendererVOArrayProvider:Array;
		/**
		 *Array<TreeCellRenderer> 递归存放
		 */
		protected var treeCellRendererArray:Array;
		/**
		 *顺序存放的所有 TreeCellRenderer
		 */
		protected var treeCellRendererList:Array;
		protected var treeContainer:Sprite;
		
		private var _treeWidth:Number = 0;
		private var _dataProvider:*;
		private var _itemRenderer:*;
		private var _treeSkin:TreeSkin;
		public function Tree() {
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			this.initTree();
		}
		
		override public function set skin(value:BaseSkin):void
		{
			_treeSkin = value as TreeSkin;
			super.skin;
		}
		
		override public function dispose():void
		{
			removeListener();
			super.dispose();
		}
		
		public function get itemRenderer():* {
			return _itemRenderer;
		}
		
		public function set itemRenderer(value:*):void {
			_itemRenderer = value;
		}
		
		public function get treeWidth():Number {
			return _treeWidth;
		}
		
		public function set treeWidth(value:Number):void {
			_treeWidth = value;
		}
		
		public function get dataProvider():* {
			return _dataProvider;
		}
		
		/**
		 *设置数据，可以是xml 或者 array
		 * @param value 数据源
		 *
		 */
		public function set dataProvider(value:*):void {
			_dataProvider = value;
			
			if (value is XML) {
				this.xmlProvider = value;
				this.resolveXMLData(value);
				return ;
			}
			
			if (value is Array) {
				this.treeCellRendererVOArrayProvider = value;
				this.resolveArrayData(value);
				return ;
			}
		}
		/**
		 *初始化 
		 * 
		 */		
		protected function initTree():void {
			this.initListeners();
		}
		
		protected function initListeners():void {
			this.addEventListener(TreeEvent.CLICK_NODE, onClickNode);
		}
		
		protected function removeListener():void {
			this.removeEventListener(TreeEvent.CLICK_NODE, onClickNode);
		}
		
		/**
		 *点击了某一个节点的处理
		 * @param e
		 *
		 */
		protected function onClickNode(e:TreeEvent):void {
			this.drawLayout();
			this.resetSelected(e);
		}
		
		/**
		 *重新设置下选中哪个
		 *
		 */
		protected function resetSelected(e:TreeEvent):void {
			for each (var treecell:TreeCellRenderer in this.treeCellRendererList) {
				if (e.target != treecell) {
					treecell.unSelected();
				}
			}
		}
		
		/**
		 *绘制布局
		 *<p>抛出改变tree高度的事件
		 * 
		 */
		protected function drawLayout():void {
			for (var i:int = 0; i < this.treeCellRendererArray.length; i++) {
				var childDis:TreeCellRenderer = this.treeCellRendererArray[i];
				if (i == 0) {
					childDis.y = 0;
				} else {
					childDis.y = this.treeCellRendererArray[i - 1].y + this.treeCellRendererArray[i -
						1].height;
				}
			}
			this.dispatchEvent(new TreeEvent(TreeEvent.CHANGE_HEIGHT,this.height));
		}
		
		/**
		 *这里绘制 UI
		 *
		 */
		protected function drawUI():void {
			if(treeContainer)
			{
				for(var j:int = 0;j<treeContainer.numChildren;j++)
				{
					this.treeContainer.removeChildAt(0);
				}
				if(this.contains(treeContainer))
				{
					this.removeChild(treeContainer);
				}
			}
			treeContainer = null;
			this.treeContainer = new Sprite();
			this.addChild(this.treeContainer);
			
			for (var i:int = 0; i < this.treeCellRendererArray.length; i++) {
				var childDis:TreeCellRenderer = this.treeCellRendererArray[i];
				
				this.treeContainer.addChild(childDis);
			}
			this.drawLayout();
			this.updateAllNode();
		}
		
		protected function updateAllNode():void {
			for each (var i:TreeCellRenderer in this.treeCellRendererList) {
				i.updateRenderer(this._treeWidth);
			}
		}
		
		/**
		 *这里进行XMl转换为TreeCellRendererVO数组的操作
		 * @param dataXML
		 *
		 */
		protected function resolveXMLData(dataXML:XML):void {
			this.treeCellRendererVOArrayProvider = [];
			
			for each (var node:XML in dataXML.elements()) {
				this.treeCellRendererVOArrayProvider.push(this.splitXMLDataToVO(null, node));
			}
			this.resolveArrayData(this.treeCellRendererVOArrayProvider);
		}
		
		/**
		 *递归解析
		 * 将XML解析为Array数组
		 * @param parentVo
		 * @param data
		 * @return
		 *
		 */
		protected function splitXMLDataToVO(parentVo:TreeCellRendererVO, data:XML):TreeCellRendererVO {
			var treeCellRendererVO:TreeCellRendererVO = new TreeCellRendererVO();
			
			treeCellRendererVO.label = data.@label;
			
			for each (var attribute:XML in data.attributes()) {
				var attriName:String = String(attribute.name());
				var attriValue:String = String(attribute.toString());
				
				if (attriName != "label") {
					treeCellRendererVO[attriName] = attriValue;
				}
			}
			if (parentVo != null) {
				treeCellRendererVO.parentNode = parentVo;
			}
			
			if (data.elements().length() > 0) {
				treeCellRendererVO.childNodes = [];
				
				for each (var node:XML in data.elements()) {
					treeCellRendererVO.childNodes.push(this.splitXMLDataToVO(treeCellRendererVO,
						node));
				}
			}
			return treeCellRendererVO;
		}
		
		/**
		 *转换 
		 * @param arrayTreeCellRendererVO
		 * 
		 */		
		protected function resolveArrayData(arrayTreeCellRendererVO:Array):void {
			this.treeCellRendererArray = [];
			this.treeCellRendererList = [];
			
			for each (var nodeVO:TreeCellRendererVO in arrayTreeCellRendererVO) {
				this.treeCellRendererArray.push(this.splitVOToRenderer(0, null, nodeVO));
			}
			
			this.drawUI();
		}
		
		/**
		 *递归解析
		 * @param parentRenderer
		 * @param data
		 * @return
		 *
		 */
		protected function splitVOToRenderer(level:int, parentRenderer:TreeCellRenderer, data:TreeCellRendererVO):TreeCellRenderer {
			var treeCellRenderer:TreeCellRenderer;
//			if (this._itemRenderer == null) {
				treeCellRenderer = new TreeCellRenderer();
//			} else {
//				var renderer:Class = getDefinitionByName(getQualifiedClassName(this._itemRenderer))as
//					Class;
//				treeCellRenderer = new renderer()as TreeCellRenderer;
//			}
			treeCellRenderer.setNodeSkin(_treeSkin);
			if (parentRenderer != null) {
				treeCellRenderer.parentNode = parentRenderer;
			}
			treeCellRenderer.level = level;
			
			if (data.hasChildNodes) {
				treeCellRenderer.childNodes = [];
				++level;
				this.maxLevel = level;
				
				for each (var nodeVO:TreeCellRendererVO in data.childNodes) {
					treeCellRenderer.childNodes.push(this.splitVOToRenderer(level, treeCellRenderer,
						nodeVO));
				}
				treeCellRenderer.drawChildrenDisplay();
			}
			treeCellRenderer.data = data;
			
			this.treeCellRendererList.push(treeCellRenderer);
			return treeCellRenderer;
			
		}
		
	}
}